package com.realworld.controller;

import cn.hutool.core.bean.BeanUtil;
import com.realworld.common.R;
import com.realworld.model.dto.UserInfoResp;
import com.realworld.model.pojo.UserInfo;
import com.realworld.service.UserInfoService;
import com.realworld.service.userAttentionService;
import com.realworld.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("profiles")
@Api(tags = "用户关注粉丝")
public class UserAttentionController {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private userAttentionService userAttentionService;

    @GetMapping("{username}")
    public R getUserByUsername(@PathVariable String username){
        UserInfo userInfo = userInfoService.getUserInfo(username);
        UserInfoResp userInfoResp = new UserInfoResp();
        BeanUtil.copyProperties(userInfo,userInfoResp);
        return R.ok(userInfoResp);
    }

    @PostMapping()
    public R FollowUser(@RequestParam("username") String username, HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        return userAttentionService.followUser(claims.getSubject(),username);
    }

    @DeleteMapping("{username}")
    public R UnFollowUser(@PathVariable String username, HttpServletRequest httpServletRequest) throws Exception {
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtil.parseJWT(token);
        return userAttentionService.UnFollowUser(claims.getSubject(),username);
    }

}
